using ServerSide.Service;
using Microsoft.Extensions.FileProviders;
using Microsoft.EntityFrameworkCore;
using ServerSide.EFCoreContext;
using ServerSide.utils;
using ServerSide.Service.Interface;
using ServerSide.MiddlewareConfigs;
namespace ServerSide
{
    public class Program
    {

        public static void Main(string[] args)
        {
            
            
            var builder = WebApplication.CreateBuilder(args);
            //EFcore连接sqlite链接并依赖注入
            builder.Services.AddDbContext<MyDBContext>(
                 options => options.UseSqlite(
                       builder.Configuration.GetConnectionString("SqliteConnection")
                 )
            );
            builder.Services.AddControllers();
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            //响应数据压缩
            builder.Services.AddResponseCompression();
            //添加跨域策略
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", opt => opt.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            });
            builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));
            #region 依赖注入
            {
                builder.Services.AddSingleton(builder.Configuration);
                builder.Services.AddLogging();
                builder.Services.AddTransient<IUserLoginService, UserLoginService>();
                builder.Services.AddTransient<IUserService, UserService>();
                builder.Services.AddTransient<IRoleService, RoleService>();
                builder.Services.AddTransient<IMenuService, MenuService>();
                builder.Services.AddScoped<TokenUtil>();
            }
            #endregion

            var app = builder.Build();
            app.UseResponseCompression();

            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();
            
            app.UseAuthorization();
            //设置静态资源访问路径
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Resources/Picture/headPortrait")),
                RequestPath = "/" + "Resources/Picture/headPortrait" // 设置访问路径的前缀
            });
            app.UseAuthorization();
            app.MapControllers();
            app.UseCors("CorsPolicy");
            app.Run();
        }
    }
}